<?php
/*
客户管理
*/
//define('Acache_no','1');
class Con_members extends Acan{
	public $defaultAct='list';
	public $Conread = '会员';
	public $Actread = array('list'=>'所有会员','my'=>'我的会员','info'=>'会员信息','pool'=>'会员池','pool_add'=>'添加为我的会员','import'=>'导入会员','import_list'=>'导入排版','import_save'=>'导入保存','save'=>'保存','del'=>'删除','delall'=>'删除所有','forgo'=>'放弃客户');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$mg = new Mongo();
		$db = $mg->Acan;
		$this->model = Afun_db()->model(Acan_table_weibo_userinfo);
		$this->mongo = $db->weibo;
	}
	//--所有客户
	function Act_list(){
		if(empty($this->atitle)){$this->atitle = '会员列表';}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$obj = Afun_acan_new('list');
		//获取key
		$index = $this->mongo->getIndexInfo(array('unique'=>1));
		foreach ($index as $k => $v) {
			if($v['unique']==1){
				$arr = array_keys($v['key']);
				$key_name = $arr[0];
			}
		}
		$index = $this->mongo->findOne();
		$delcol = array('_id','sex','area','avatar','avatar_min','birthday','birthday_date','xinzuo','follow','fans','weibo','mark','status','getdate');
		$columnrr = Afun_arr_remove($this->model->comment,array('mark'));//初始栏目
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Awform');
		$column_json = $obj->form_json($this->model);
		$column_sop = Afun_arr_remove($this->model->comment,$delcol);
		$all[''] = '所有';
		$sex = $all+$this->model->enum['sex'];
		$status = $all+$this->model->enum['status'];
		$xinzuo = $all+$this->model->enum['xinzuo'];
		$where = $this->search($area_val,$post,Afun_arr_remove($index,$delcol));
		$area_val = Ajson_encode($area_val);
		//分页函数
		if($this->Aact=='list'){
			//$con->where = "1".$where;
		}else{
			//$con->where = "a.uid=$uid".$where;
		}
		//$con->select = "a.*";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $key_name;
		$page->page = '10';
		$qstart=gettime();
		$page->counts = $this->mongo->count($where);	//统计总数目
		echo round(gettime() - $qstart,6).'s<br>';
		$pages = Afun_apage($page);
		//$con->order = "a.$pages->orderby";
		//$con->limit = $pages->limit;
		//print_r($pages);
		var_dump($where);
		//分页结束
		if(is_array($where)){
			$list = $this->mongo->find($where)->limit($pages->page);
		}else{
			$list = $this->mongo->find()->limit($pages->page);
		}
		$list->sort($pages->ordermg);
		if(is_object($list))
		foreach($list as $k=>$v){
			$listjson[$v[$key_name]] = $v;
		}
		$listjson = Ajson_encode($listjson);
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$info = $this->model->ofindPk($this->id);
		Afun_json($info);
	}
	function search(&$area_val,&$post,$column_sop){
		if($_GET){
			$post = $_GET;
			$area_val = array();
			if(strlen($post['xinzuo'])>0){
				$where['xinzuo'] = $post['xinzuo'];
			}
			if(strlen($post['sex'])>0){
				$where['sex'] = $post['sex'];
			}
			if(strlen($post['status'])>0){
				$where['status'] = $post['status'];
			}
			if($post['shi']>1){
				$where['area']['shi'] = $post['shi'];
				$where['area']['sheng'] = $post['sheng'];
				$area_val[1] = $post['sheng'];
				$area_val[2] = $post['shi'];
			}elseif($post['sheng']>1){
				$where['area']['sheng'] = $post['sheng'];
				$area_val[1] = $post['sheng'];
			}
			if(!empty($post['search'])){
				$qex = new MongoRegex("/".$post['search']."/");
				$sop = $post['sop'];
				if($post['sop']!=''){
					$where[$post['sop']] = $qex;
				}else{
					foreach($column_sop as $k=>$v){
						$where['$or'][][$k] = $qex;
					}
				}
			}
		}
		return $where;
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一个新用户
				$msg = $this->model->save($savedata);
			}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		Afun_json($msg);
	}
}

?>